'use strict';

angular.module('memoriesApp')
.factory('toastService', function($timeout) {

  var toast = window.plugins && window.plugins.toast;

  // open toast
  function _openToast(toastParam, callback) {
    if (!toast) {
      callback && callback(true, 'Toast plugin is not available!');
      return;
    }
    var message = toastParam.message || 'useless message';
    var duration = toastParam.duration || 'short';
    if (duration != 'short' && duration != 'long') {
      duration = 'short';
    }
    var position = toastParam.position || 'center';
    if (position != 'bottom' && position != 'top' && position != 'center') {
      position = 'center';
    }
    var delay = toastParam.delay || 700;

    $timeout(function() {
      toast.show(message, duration, position, function(msg) {
        callback && callback(null, msg);
      }, function(err) {
        callback && callback(err);
      });
    }, delay);
  }

  return {
    openToast: _openToast
  };

});